package com.wx.wxapp.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.wxapp.dao.FoodMapper;
import com.wx.wxapp.dao.FoodimageMapper;
import com.wx.wxapp.dao.UserMapper;
import com.wx.wxapp.entity.Food;
import com.wx.wxapp.entity.Foodimage;
import com.wx.wxapp.entity.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class FoodService {
    @Autowired
    private SqlSessionFactory sqlSessionFactory;


    /*
     *   功能：获取用户关于食物经验的发帖纪录
     *   参数：
     *       openid:用户的openid
     * */
    @Transactional
    public List<List<String>> getHistoryFoodCard(String openid){
        List<List<String>> foodList = new ArrayList<>();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        FoodMapper foodMapper = sqlSession.getMapper(FoodMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        FoodimageMapper foodimageMapper =sqlSession.getMapper(FoodimageMapper.class);
        User user = userMapper.selectByOpenid(openid);
        int id = user.getId();
        List<Food> foodCards = foodMapper.selectByUserId(id);
        Iterator<Food> it1 = foodCards.iterator();
        FoodCard(foodList, foodimageMapper, it1);
        sqlSession.close();
        return foodList;
    }



    /*
     *   功能：获取food帖子
     *   参数：
     * */
    @Transactional
    public Map getFoodCard(int pageNumber){
        List<List<String>> foodList = new ArrayList<>();
        Map map = new HashMap();
        PageHelper.startPage(pageNumber,4);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        FoodMapper foodMapper = sqlSession.getMapper(FoodMapper.class);
        FoodimageMapper foodimageMapper = sqlSession.getMapper(FoodimageMapper.class);
        List<Food> foods = foodMapper.selectAll();
        PageInfo<Food> pageInfo = new PageInfo<>(foods);
        int size = pageInfo.getPages();
        Iterator<Food> it1 = foods.iterator();
        FoodCard(foodList, foodimageMapper, it1);
        sqlSession.close();
        map.put("list",foodList);
        map.put("size",size);
        return map;
    }
    public void FoodCard(List<List<String>> foodList, FoodimageMapper foodimageMapper, Iterator<Food> it1) {
        while (it1.hasNext()){
            List<String> list = new ArrayList<>();
            Food food = it1.next();
            list.add(food.getId());list.add(food.getTitle());list.add(food.getInformation());list.add(food.getUsername());list.add(food.getImage());list.add((food.getPraisenumber()).toString());list.add(food.getLabel());
            String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(food.getTime());
            list.add(date);
            String cardId = food.getId();
            List<Foodimage> foodimages = foodimageMapper.selectByCardId(cardId);
            Iterator<Foodimage> it2 = foodimages.iterator();
            while (it2.hasNext()){
                list.add(it2.next().getImagename());
            }
            foodList.add(list);
        }
    }

}
